package com.classic.okhttp.services;

import com.classic.okhttp.Cons;
import android.app.Activity;
import com.classic.okhttp.base.OkHttpUtils;
import com.classic.okhttp.zmoumall.utils.MD5;
import com.classic.okhttp.zmoumall.http.ACache;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.classic.okhttp.zmoumall.http.ObjectCallback;
import okhttp3.MediaType;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

import com.classic.okhttp.beans.HVVersionBean;
import com.classic.okhttp.beans.HVCitiesBean;
import com.classic.okhttp.beans.HVProjectBean;
import com.classic.okhttp.beans.HVHomePageItemBean;


import com.classic.okhttp.enums.Enum;

public class ICommonService
{

	private static String _getFullURL(String method)
	{
		return new StringBuffer( Cons.GATEWAY_URL ).append("common/").append(method).toString();
	}

	//版本更新
	public static void checkVersion(Activity activity, String version, String channel, Enum.EnumPlatform platform, boolean ignoreLoginLog, boolean isCache, ObjectCallback<HVVersionBean> callback)
	{
		callback.setCallBackBean(new TypeToken<HVVersionBean>(){}.getType());
		callback.setContext(activity);

		Map<String, Object> params = new HashMap<>();
		params.put("version", version);
		params.put("channel", channel);
		params.put("platform", platform.value);
		params.put("ignoreLoginLog", ignoreLoginLog);

		Gson gson=new Gson();
		String json=gson.toJson(params);

		String url = _getFullURL("checkVersion");

		if(isCache) {//添加缓存
			String key = MD5.MD5Encode(new StringBuffer(url).append(version).append(channel).append(platform).append(ignoreLoginLog).toString());
			ACache aCache = ACache.get(activity);
			callback.setACache(aCache, key, isCache);
		}

		OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json;charset=utf-8"))
               	.url(url)
                .content(json)
                .addHeader("Token",Cons.mToken)
                .addHeader("DeviceID",Cons.mDeviceId)
				.addHeader("DeviceType",Cons.mDeviceType)
				.addHeader("Version",Cons.mVersion)
				.addHeader("VersionCode",Cons.mVersionCode)
				.addHeader("Data",Cons.getData(json))
				.addHeader("AppType",Cons.getAppType())
                .tag(activity)
                .build()
                .execute(callback);
	}

	//获取区域信息
	public static void getCities(Activity activity, boolean isCache, ObjectCallback<ArrayList<HVCitiesBean>> callback)
	{
		callback.setCallBackBean(new TypeToken<ArrayList<HVCitiesBean>>(){}.getType());
		callback.setContext(activity);

		Map<String, Object> params = new HashMap<>();

		Gson gson=new Gson();
		String json=gson.toJson(params);

		String url = _getFullURL("getCities");

		if(isCache) {//添加缓存
			String key = MD5.MD5Encode(new StringBuffer(url).toString());
			ACache aCache = ACache.get(activity);
			callback.setACache(aCache, key, isCache);
		}

		OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json;charset=utf-8"))
               	.url(url)
                .content(json)
                .addHeader("Token",Cons.mToken)
                .addHeader("DeviceID",Cons.mDeviceId)
				.addHeader("DeviceType",Cons.mDeviceType)
				.addHeader("Version",Cons.mVersion)
				.addHeader("VersionCode",Cons.mVersionCode)
				.addHeader("Data",Cons.getData(json))
				.addHeader("AppType",Cons.getAppType())
                .tag(activity)
                .build()
                .execute(callback);
	}

	//获取运动项目
	public static void getProjects(Activity activity, String cityCode, boolean isCache, ObjectCallback<ArrayList<HVProjectBean>> callback)
	{
		callback.setCallBackBean(new TypeToken<ArrayList<HVProjectBean>>(){}.getType());
		callback.setContext(activity);

		Map<String, Object> params = new HashMap<>();
		params.put("cityCode", cityCode);

		Gson gson=new Gson();
		String json=gson.toJson(params);

		String url = _getFullURL("getProjects");

		if(isCache) {//添加缓存
			String key = MD5.MD5Encode(new StringBuffer(url).append(cityCode).toString());
			ACache aCache = ACache.get(activity);
			callback.setACache(aCache, key, isCache);
		}

		OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json;charset=utf-8"))
               	.url(url)
                .content(json)
                .addHeader("Token",Cons.mToken)
                .addHeader("DeviceID",Cons.mDeviceId)
				.addHeader("DeviceType",Cons.mDeviceType)
				.addHeader("Version",Cons.mVersion)
				.addHeader("VersionCode",Cons.mVersionCode)
				.addHeader("Data",Cons.getData(json))
				.addHeader("AppType",Cons.getAppType())
                .tag(activity)
                .build()
                .execute(callback);
	}

	//获取首页内容
	public static void getHomepageContent(Activity activity, String cityCode, boolean isCache, ObjectCallback<ArrayList<HVHomePageItemBean>> callback)
	{
		callback.setCallBackBean(new TypeToken<ArrayList<HVHomePageItemBean>>(){}.getType());
		callback.setContext(activity);

		Map<String, Object> params = new HashMap<>();
		params.put("cityCode", cityCode);

		Gson gson=new Gson();
		String json=gson.toJson(params);

		String url = _getFullURL("getHomepageContent");

		if(isCache) {//添加缓存
			String key = MD5.MD5Encode(new StringBuffer(url).append(cityCode).toString());
			ACache aCache = ACache.get(activity);
			callback.setACache(aCache, key, isCache);
		}

		OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json;charset=utf-8"))
               	.url(url)
                .content(json)
                .addHeader("Token",Cons.mToken)
                .addHeader("DeviceID",Cons.mDeviceId)
				.addHeader("DeviceType",Cons.mDeviceType)
				.addHeader("Version",Cons.mVersion)
				.addHeader("VersionCode",Cons.mVersionCode)
				.addHeader("Data",Cons.getData(json))
				.addHeader("AppType",Cons.getAppType())
                .tag(activity)
                .build()
                .execute(callback);
	}

	//获取首页弹出式banner
	public static void getHomeBanner(Activity activity, String cityCode, boolean isCache, ObjectCallback<HVHomePageItemBean> callback)
	{
		callback.setCallBackBean(new TypeToken<HVHomePageItemBean>(){}.getType());
		callback.setContext(activity);

		Map<String, Object> params = new HashMap<>();
		params.put("cityCode", cityCode);

		Gson gson=new Gson();
		String json=gson.toJson(params);

		String url = _getFullURL("getHomeBanner");

		if(isCache) {//添加缓存
			String key = MD5.MD5Encode(new StringBuffer(url).append(cityCode).toString());
			ACache aCache = ACache.get(activity);
			callback.setACache(aCache, key, isCache);
		}

		OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json;charset=utf-8"))
               	.url(url)
                .content(json)
                .addHeader("Token",Cons.mToken)
                .addHeader("DeviceID",Cons.mDeviceId)
				.addHeader("DeviceType",Cons.mDeviceType)
				.addHeader("Version",Cons.mVersion)
				.addHeader("VersionCode",Cons.mVersionCode)
				.addHeader("Data",Cons.getData(json))
				.addHeader("AppType",Cons.getAppType())
                .tag(activity)
                .build()
                .execute(callback);
	}

	//获取标签列表
	public static void getTagList(Activity activity, Enum.EnumItemType tagType, boolean isCache, ObjectCallback<ArrayList<String>> callback)
	{
		callback.setCallBackBean(new TypeToken<ArrayList<String>>(){}.getType());
		callback.setContext(activity);

		Map<String, Object> params = new HashMap<>();
		params.put("tagType", tagType.value);

		Gson gson=new Gson();
		String json=gson.toJson(params);

		String url = _getFullURL("getTagList");

		if(isCache) {//添加缓存
			String key = MD5.MD5Encode(new StringBuffer(url).append(tagType).toString());
			ACache aCache = ACache.get(activity);
			callback.setACache(aCache, key, isCache);
		}

		OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json;charset=utf-8"))
               	.url(url)
                .content(json)
                .addHeader("Token",Cons.mToken)
                .addHeader("DeviceID",Cons.mDeviceId)
				.addHeader("DeviceType",Cons.mDeviceType)
				.addHeader("Version",Cons.mVersion)
				.addHeader("VersionCode",Cons.mVersionCode)
				.addHeader("Data",Cons.getData(json))
				.addHeader("AppType",Cons.getAppType())
                .tag(activity)
                .build()
                .execute(callback);
	}

	//上传地理位置和区号
	public static void uploadCityCode(Activity activity, String cityCode, String deviceId, String pushClientId, String address, int platform, boolean isCache, ObjectCallback<Object> callback)
	{
		callback.setCallBackBean(new TypeToken<Object>(){}.getType());
		callback.setContext(activity);

		Map<String, Object> params = new HashMap<>();
		params.put("cityCode", cityCode);
		params.put("deviceId", deviceId);
		params.put("pushClientId", pushClientId);
		params.put("address", address);
		params.put("platform", platform);

		Gson gson=new Gson();
		String json=gson.toJson(params);

		String url = _getFullURL("uploadCityCode");

		if(isCache) {//添加缓存
			String key = MD5.MD5Encode(new StringBuffer(url).append(cityCode).append(deviceId).append(pushClientId).append(address).append(platform).toString());
			ACache aCache = ACache.get(activity);
			callback.setACache(aCache, key, isCache);
		}

		OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json;charset=utf-8"))
               	.url(url)
                .content(json)
                .addHeader("Token",Cons.mToken)
                .addHeader("DeviceID",Cons.mDeviceId)
				.addHeader("DeviceType",Cons.mDeviceType)
				.addHeader("Version",Cons.mVersion)
				.addHeader("VersionCode",Cons.mVersionCode)
				.addHeader("Data",Cons.getData(json))
				.addHeader("AppType",Cons.getAppType())
                .tag(activity)
                .build()
                .execute(callback);
	}

	//上报分享成功次数
	public static void reportShareRecord(Activity activity, Enum.EnumShareTypeEnum shareType, boolean isCache, ObjectCallback<Object> callback)
	{
		callback.setCallBackBean(new TypeToken<Object>(){}.getType());
		callback.setContext(activity);

		Map<String, Object> params = new HashMap<>();
		params.put("shareType", shareType.value);

		Gson gson=new Gson();
		String json=gson.toJson(params);

		String url = _getFullURL("reportShareRecord");

		if(isCache) {//添加缓存
			String key = MD5.MD5Encode(new StringBuffer(url).append(shareType).toString());
			ACache aCache = ACache.get(activity);
			callback.setACache(aCache, key, isCache);
		}

		OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json;charset=utf-8"))
               	.url(url)
                .content(json)
                .addHeader("Token",Cons.mToken)
                .addHeader("DeviceID",Cons.mDeviceId)
				.addHeader("DeviceType",Cons.mDeviceType)
				.addHeader("Version",Cons.mVersion)
				.addHeader("VersionCode",Cons.mVersionCode)
				.addHeader("Data",Cons.getData(json))
				.addHeader("AppType",Cons.getAppType())
                .tag(activity)
                .build()
                .execute(callback);
	}
}
